Typical HAL CRC use cases for migration

This section provides information about typical HAL CRC use cases for migration.

Use case: CRC calculation with default configuration

This use case describes how to calculate a CRC with the default configuration using HAL1 and HAL2.

Note

Once the CRC peripheral is initialized, the calculation process can be started without calling the global configuration function.

The default parameters are already set:

  • Default polynomial

  • Default CRC init value

  • Input reversibility none

  • Output reversibility disabled

Sequence diagram

CRC calculation with default configuration (polling mode)

  @startuml

  == Initialization ==

  "Application"->"CRC" : HAL_CRC_Init()
  "CRC" --> "Application" : hal status : HAL_OK

  == Process : Polling mode ==

  "Application"->"CRC" :  HAL_CRC_Calculate
  "CRC" --> "Application": hal status : HAL_OK

  @enduml

Code snippet

HAL1

HAL2

__IO uint32_t uwCRCValue = 0;
HAL_CRC_Init(&hcrc);
uwCRCValue = HAL_CRC_Calculate(&hcrc,
                               (uint32_t *)&aDataBuffer,
                               BUFFER_SIZE);
/* */
HAL_CRC_Init(&hCRC, HAL_CRC);
HAL_CRC_Calculate(pCRC,
                  &DataBuffer,
                  sizeof(uint32_t) * BUFFER_SIZE,
                  &CrcValue);

Key changes

  1. In HAL2, CRC calculation uses a handle initialized with an explicit instance (for example HAL_CRC_Init(&hCRC, HAL_CRC)).

  2. In HAL2, CRC APIs can return additional statuses (for example HAL_INVALID_PARAM and HAL_BUSY).

  3. The calculation call signature differs (buffer pointer and size in bytes, and output value provided as a parameter).

Use case: CRC calculation with user configuration

This use case describes how to calculate a CRC with a user configuration using HAL1 and HAL2.

Sequence diagram

CRC calculation with user configuration (polling mode)

  @startuml

  == Initialization ==

  "Application"->"CRC" : HAL_CRC_Init()
  "CRC" --> "Application" : hal status : HAL_OK

  == Process : Polling mode ==

  "Application"->"CRC" :  HAL_CRC_Calculate
  "CRC" --> "Application": hal status : HAL_OK

  @enduml

Code snippet

HAL1

HAL2

HAL_CRC_Init(&hcrc);

uwCRCValue = HAL_CRC_Calculate(&hcrc,
                               (uint32_t *)&aDataBuffer,
                               BUFFER_SIZE);
HAL_CRC_Init(&hCRC, HAL_CRC);
HAL_CRC_SetConfig(&hCRC, &crc_config);
HAL_CRC_Calculate(pCRC,
                  &DataBuffer,
                  sizeof(uint32_t) * BUFFER_SIZE,
                  &CrcValue);

Key changes

  1. In HAL2, user configuration is applied through HAL_CRC_SetConfig() before starting the calculation.

  2. In HAL2, the CRC APIs can return additional statuses (for example HAL_INVALID_PARAM and HAL_BUSY).


Use case: CRC accumulate

This sequence diagram illustrates a CRC calculation by chunks.

Accumulation is required when the input data is not entirely accessible, allowing for processing data chunks as they become available.

Sequence diagram

CRC accumulation by chunks (polling mode)

  @startuml

  == Initialization ==

  "Application" -> "CRC" : HAL_CRC_Init()
  "CRC" --> "Application" : hal status : HAL_OK or HAL_INVALID_PARAM

  == Process : Polling mode ==

  "Application" -> "CRC" : HAL_CRC_Calculate
  "CRC" --> "Application" : HAL_OK
  ...
  ...
  ...
  ...
  ...

  "Application" -> "CRC" : HAL_CRC_Accumulate
  "CRC" --> "Application" : HAL_OK

  @enduml

Code snippet

HAL1

HAL2

HAL_CRC_Init(&hcrc);




uwCRCValue = HAL_CRC_Calculate(&hcrc,
                               (uint32_t *)&aDataBuffer,
                               BUFFER_SIZE);



uwCRCAccValue = HAL_CRC_Accumulate(&hcrc,
                                (uint32_t *)&aDataBuffer,
                                BUFFER_SIZE)
HAL_CRC_Init(&hCRC, HAL_CRC);

/* The HAL_CRC_SetConfig() is not required when the default
configuration is applied */
HAL_CRC_SetConfig(&hCRC, &crc_config);

HAL_CRC_Calculate(pCRC,
                  &DataBuffer,
                  sizeof(uint32_t) * BUFFER_SIZE,
                  &CrcValue);


HAL_CRC_Accumulate(pCRC,
                  &DataBuffer,
                  sizeof(uint32_t) * BUFFER_SIZE,
                  &CrcValue)

Key changes

  1. In HAL2, configuration through HAL_CRC_SetConfig() is optional when the default configuration is applied.

  2. In HAL2, HAL_CRC_Calculate() and HAL_CRC_Accumulate() can return additional statuses (for example HAL_INVALID_PARAM and HAL_BUSY).

Related examples in HAL2

Example name

Brief

example_hal_crc_16bit_reversed

This example illustrates setting up the CRC unit via the HAL API to calculate a 16-bit CRC from 32-bit or 8-bit data, with data reversal enabled and a custom polynomial of 0x1021.

example_hal_crc_user_defined_polynomial

This example demonstrates how to configure the CRC unit to compute an 8-bit CRC code using a user-defined polynomial.

example_hal_crc_7bit_stream

This example is used to configure the CRC unit via the HAL API to compute 7-bit CRC codes from 8-bit data buffers, using a user-defined polynomial set to 0x65.